package com.exam.examtest.presenter

import android.content.Context
import com.exam.examtest.bean.HttpRecordBean
import com.exam.examtest.contract.MainContract
import com.exam.examtest.model.MainModel
import com.exam.examtest.util.ConstantValue
import com.exam.examtest.util.DBHelper
import com.exam.examtest.util.IHttpCallback

/**
 * ClassName MainPresenter
 * Create by rhf
 * Create on 2021/3/30 17:02
 * Description 主页面业务逻辑层
 */
class MainPresenter(mContext: Context,mView: MainContract.View) : MainContract.Presenter {

    var mModel: MainContract.Model? = null
    private var mView: MainContract.View? = null
    private var mContext: Context? = null

    init {
        mModel = MainModel()
        this.mView = mView
        this.mContext = mContext
    }

    override fun httpData() {
        val httpRecordBean = HttpRecordBean()
        httpRecordBean.type = ConstantValue.TYPE_HTTP
        httpRecordBean.saveUrlOrData = ConstantValue.BASE_URL
        httpRecordBean.httpTimeStamp = System.currentTimeMillis()
        DBHelper.getInstance().saveData(httpRecordBean)//保存请求记录

        val callback = object : IHttpCallback {
            override fun onSuccess(result: String) {
                mView?.handleResult(result)
            }

            override fun onFail(errorMessage: String) {

            }

        }
        mModel?.getData(mContext,callback)
    }
}